#include <string.h>
#include <fstream>
#include "contacts.pb.h"


void AddPeopleInfo(contactsname::PeopleInfo* people)
{
  std::cout << "----------新增联系人----------" << std::endl;
  std::cout << "请输入联系人名称: ";
  std::string name;
  std::getline(std::cin, name);
  //设置联系人名称
  people->set_name(name);

  std::cout << "请输入联系人年龄: " ;
  int age;
  std::cin >> age;
  //设置联系人年龄
  people->set_age(age);

  std::cin.ignore(256,'\n');

  //添加电话
  for (int i = 0; ; ++i)
  {
    std::cout << "请输入电话号码" << i + 1 << ": ";
    std::string number;
    std::getline(std::cin, number);
    if (number.empty())
        break;
    
    //开空间
    contactsname::PeopleInfo_Phone * phone = people->add_phone();
    phone->set_number(number);

    std::cout << "请输入电话类型:(1. 移动电话  2. 固定电话):";
    int type;
    std::cin >> type;
    //忽略换行符
    std::cin.ignore(256, '\n');
    switch (type)
    {
      case 1: 
          phone->set_type(contactsname::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
          break;
      case 2:
          phone->set_type(contactsname::PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
          break;
      default:
          std::cout << "输入错误" << std::endl;
          break;
    }

  }

    contactsname::Adress adress;
    std::cout << "请输入联系人家庭地址";
    std::string home_address;
    std::getline(std::cin, home_address);
    //设置联系人家庭地址
    adress.set_home_address(home_address);

    std::cout << "请输入联系人单位地址";
    std::string unit_address;
    std::getline(std::cin, unit_address);
    //设置联系人单位地址
    adress.set_unit_address(unit_address);

    //address to Any 添加联系人到Any
    people->mutable_data()->PackFrom(adress);

    //添加联系人其他联系方式
    std::cout << "请选择要添加的其他联系方式(1.QQ     2.微信号):";
    int other_contact;
    std::cin >> other_contact;
    std::cin.ignore(256, '\n');
    if (1 == other_contact)
    {
      std::string qq;
      std::cout << "请输入联系人qq号";
      std::getline(std::cin, qq);
      people->set_qq(qq);
      std::cout << "设置qq号码成功" << std::endl;
    }
    else if (2 == other_contact)
    {
      std::cout << "请输入联系人微信号";
      std::string wechat;
      std::getline(std::cin, wechat);
      people->set_wechat(wechat);
      std::cout << "设置微信号码成功" << std::endl;
    }
    else
    {
      std::cout << "输入有错误" << std::endl;
    }

    //添加联系人备注
    std::cout << "按回车结束备注新增" << std::endl;
    for (int  i = 0; ; ++i)
    {
      std::cout << "请输入备注标题" << i + 1 << ":";
      std::string title;
      std::getline(std::cin, title);
      if (title.empty()) break;

      std::cout << "请输入备注 " << i + 1 << ":";
      std::string content;
      std::getline(std::cin, content);
      //将其设置进remark
      people->mutable_remark()->insert({title, content});
    }

  std::cout << "----------添加联系人成功--------" << std::endl;
}



int main()
{
  contactsname::contacts contact;
  //读取本地存在的文件
  std::fstream input("contacts.bin", std::ios::binary | std::ios::in);
  if (!input)
  {
    std::cout << "contacts.bin not exist, creat new file " << std::endl;
  }
  else if (!contact.ParseFromIstream(&input)) // 从文件流中读取文件并反序列化
  {
    std::cerr << "paser error" << std::endl;
    input.close();
    return -1;
  }

  //向通讯录中加入联系人
  //contacts.add_contacts 新增对象， 返回contacts类成员对象PeopleInfo指针。
  AddPeopleInfo(contact.add_contact());

  //将通讯录写入文件
  std::fstream output("contacts.bin", std::ios::out | std::ios::trunc | std::ios::binary);
  if (!contact.SerializeToOstream(&output))
  {
     std::cerr << "write error" << std::endl;
     input.close();
     output.close();
     return -2;
  }
  std::cout << "write sucess" << std::endl;

  return 0;
}